package cli.menu;

/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * DeletePaymentMode class
 * 
 * Description: 
 * 
 * 
 * @package   cli.menu
 * @author    Lai Yong Quan (A0080909Y)
 * @edited by: Tan Si Ling (A0085314J)
 */

import java.util.ArrayList;
import shared.*;
import cli.tools.Process;


class DeletePaymentMode extends Function{

	private static final String[] DELETEPM = {
		"-= Main Menu > Manage Payment Modes > Delete Payment Mode =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu",
		"",
		"Select the Payment Mode to delete by keying in its index:"	
	};

	private static final String[] SELECT = {
		"-= Main Menu > Manage Payment Modes > Delete Payment Mode =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu",
		"",
		"Would you like to select a Payment Mode to shift the items to? Y/N",
		"If you select no (N), they will automatically be placed into the 'Uncategorised' Payment Mode.",
	};

	private static final String[] DESTPM = {
	"-= Main Menu > Manage Payment Modes > Delete Payment Mode =-",
	"[b]: back [u]: undo [r]: redo [h]: home menu",
	"",
	"Select the destination Mode by keying in its index:"
	};
	
	private static State BACK = State.MANAGEPAYMENTMODES;
	private static State CURRENT = State.DELETEPAYMENTMODE;

	public static State runMenu(){

//		cli.print(DELETECAT);

		Mode toDelete;

		Object choice = tools.selectFromList(cli.getApi().getModeList(), "Payment Modes",DELETEPM);

		// if null object returned (back indicator)
		if (choice == null) {
			return BACK;
		}

		// if received Instruction object (divert to another menu)
		if (choice.getClass() == State.class) {
			return (State) choice;
		}

		// if choice is what was expected
		toDelete = (Mode) choice;



		if (toDelete.equals(cli.getApi().getModeList().get(0))) {
			cli.clearScreen();
			cli.println("Unable to delete 'Uncategorised'");
			cli.println("Returning to previous menu");
			return BACK;
		}
		
		if (toDelete.getName().equalsIgnoreCase("Loan")) {
			cli.clearScreen();
			cli.println("Unable to delete 'Uncategorised'");
			cli.println("Returning to previous menu");
			return BACK;
		}


		cli.print(SELECT);

	
		String input = cli.nextLine();
		cli.clearScreen();

		return processInput(input, toDelete);

	}


	private static State processInput(String input, Mode toDelete){

		char charInput = input.charAt(0);

		// set default destination to Uncategorised (index 0)
		Mode destination = cli.getApi().getModeList().get(0);

		switch (charInput) {

		case 'Y':
		case 'y':
			//cli.println("Select the destination Mode by keying in its index:");

			Object choice = tools.selectFromList(cli.getApi().getModeList(), "Payment Modes", DESTPM);

			// if null object returned (back indicator)
			if (choice == null) {
				return State.MANAGEPAYMENTMODES;
			}

			// if received Instruction object (divert to another menu)
			if (choice.getClass() == State.class) {
				return (State) choice;
			}

			// if choice is what was expected
			destination = (Mode) choice;
			break;

			//treat anything else other than Y and y as invalid entries
		default:
			break;

		}

		try{
			ArrayList<Record> recordList = cli.getApi().mergeMode(toDelete, destination);

			// Si Ling: **Keeping Track of the Actions DONE**
			tools.action.addToUndoStack(toDelete, destination,
					Process.MERGEMODES, recordList);
			// **END**
		}
		catch(Exception e){
			cli.println("An error has occurred - aborting");
		}

		return CURRENT;

	}
}